package cn.ronghuanet.auth.controller;

import cn.ronghuanet.auth.annotation.RonghuaPermission;
import cn.ronghuanet.auth.domain.Permission;
import cn.ronghuanet.auth.query.PermissionQuery;
import cn.ronghuanet.auth.service.IPermissionService;
import cn.ronghuanet.basic.utils.AjaxResult;
import cn.ronghuanet.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/permission")
@RonghuaPermission(name = "权限管理",descs = "权限管理")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    /**
     * 分页+高级查询
     * 分页: currentPage pageSize
     * 高级查询:keyword
     */
    @PostMapping
    @RonghuaPermission(name = "分页高级查询",descs = "分页高级查询")
    public AjaxResult pageList(@RequestBody PermissionQuery query){
        try {
            // 调用service进行分页操作,返回total  rows(当前页数据)
            PageList<Permission> pageList = permissionService.pageList(query);
            return AjaxResult.me().setResultObj(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("分页查询失败!"+e.getMessage());
        }
    }

    /**
     * 获取权限树
     * @return
     */
    @GetMapping("/tree")
    @RonghuaPermission(name = "获取权限树",descs = "获取权限树")
    public AjaxResult tree(){
        try {
            // 这个集合它是一级权限
            List<Permission> lists = permissionService.tree();
            return AjaxResult.me().setResultObj(lists);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("权限树查询失败!"+e.getMessage());
        }
    }
}
